from elasticsearch import Elasticsearch
import datetime

es = Elasticsearch(hosts='106.13.117.37', port='9200')
import pandas as pd

body = {
    "query": {
        "match_all": {}
    }
}
ll = []
data = es.search(index="movieinfo", body=body).get('hits').get('hits')
for i in data:
    ll.append(i['_source'])

df = pd.DataFrame(ll)

person_to_sell = []
kind_to_sell = []

count = 0

for name, value in df.groupby("key_movie"):
    print('运行第一个循环')
    body = {
        "query": {
            "match": {
                "movie_name": name
            }
        }
    }
    temp = es.search(index="moviecomment", body=body).get('hits').get('hits')[0]['_source']
    try:
        star = temp['movie_star_info'][0]['movie_star_name'].split(',') if ',' in temp['movie_star_info'][0][
            'movie_star_name'] else [temp['movie_star_info'][0]['movie_star_name'], ]
    except:
        count += 1
    kind = temp['movie_class'].split(',') if ',' in temp['movie_class'] else [temp['movie_class'], ]
    sells = value['boxOffice'].sum()
    for i in star:
        person_to_sell.append({'name': i, 'sell': sells})
    for i in kind:
        kind_to_sell.append({'name': i, 'sell': sells})

star_df = pd.DataFrame(person_to_sell)
kind_df = pd.DataFrame(kind_to_sell)

star_num = []
kind_num = []

for name, value in star_df.groupby("name"):
    temp = value['sell'].sum()
    star_num.append({'name': name, 'sell': temp})
for name, value in kind_df.groupby("name"):
    temp = value['sell'].sum()
    kind_num.append({'name': name, 'sell': temp})

for i in kind_num:
    name = i['name']
    body = {
        "query": {
            "match": {
                "movie_class": name
            }
        }
    }
    temp_data = es.search(index='moviecomment', body=body).get('hits').get('hits')
    movie_to_sell = []
    print('运行第二个循环')
    for j in temp_data:
        movie = j['_source']['movie_name']
        movie_body = {
            "query": {
                "match": {
                    "movie_name": movie
                }
            }
        }
        temp_sell = es.search(index='movieinfo', body=movie_body).get('hits').get('hits')
        cnt = 0
        print('运行第三个循环')
        for k in temp_sell:
            cnt += k['_source']['boxOffice']
        movie_to_sell.append({'name': movie, 'sell': cnt})
    i['movieinfo'] = movie_to_sell

try:
    es.indices.delete('kind_to_sell')
except:
    pass

create = {
    "mappings": {
        "properties": {
            "name": {
                "type": 'text'
            },
            "sell": {
                "type": "float"
            },
            "movieinfo": {
                "type": "nested"
            },
            "date": {
                "type": "date",
                "format": "yyyy-MM-dd"
            }
        }
    }
}
es.indices.create(index='kind_to_sell', body=create)
for i in kind_num:
    body = {
        'name': i['name'],
        'sell': i['sell'],
        'movieinfo': i['movieinfo'],
        "date": datetime.date.today()
    }
    es.index(index='kind_to_sell', body=body)
